<?php
defined('BASEPATH') or exit('No direct script access allowed');

class MY_Model extends CI_Model
{

    private static $_db_map = array();
    public $dbName = 'novel';
    public $tbName = '';

    public function __construct()
    {
        $this->db = self::get_db_instance($this->dbName);
    }


    public function findOne($params)
    {
        $stmt = $this->__find($params);
        return $stmt === false ? false : $stmt->row_array();
    }


    public function findAll($params)
    {
        $stmt = $this->__find($params);
        return $stmt === false ? false : $stmt->result_array();
    }

    public function del($params){
        $where = $params['where'];
        if(empty($where)){
            return false;
        }
        $this->db->where($where);
        $ret = $this->db->delete($this->tbName);
        if(false === $ret) {
            log_message('error', 'sign remind cancel:'.var_export($this->db->error(), true));
            return false;
        }
        return true;
    }

    public function update($params, $data)
    {
        if (!isset($params['where'])) {
            return false;
        }
        $this->db->where($params['where']);
        $stmt = $this->db->update($this->tbName, $data);
        if ($stmt === false) {
            log_message('error', 'update ' . $this->db->last_query() . var_export($this->db->error(), true));
            return false;
        } else {
            return $this->db->affected_rows();
        }
    }


    public function insert($data)
    {
        $result = $this->db->insert($this->tbName, $data);
        if (false === $result || 0 == $this->db->affected_rows()) {
            log_message('error', 'insert ' . $this->db->last_query() . var_export($this->db->error(), true));
            return false;
        }
        return true;
    }


    public function startTransaction($strict = false)
    {
        $this->db->trans_strict($strict);
        return $this->db->trans_begin();
    }


    public function status()
    {
        return $this->db->trans_status();
    }


    public function commit()
    {
        return $this->db->trans_commit();
    }


    public function rollback()
    {
        return $this->db->trans_rollback();
    }


    public function set($params)
    {
        if (!isset($params['where'])) {
            return false;
        }
        $this->db->where($params['where']);
        if (isset($params['set'])) {
            foreach ($params['set'] as $k => $v) {
                $this->db->set($k, $v, false);
            }
        } else {
            return false;
        }
        $stmt = $this->db->update($this->tbName);
        if ($stmt === false) {
            log_message('error', 'update ' . $this->db->last_query() . var_export($this->db->error(), true));
            return false;
        } else {
            return $this->db->affected_rows();
        }
    }


    /**
     * 数据库连接
     *
     * @param string db.name
     * @return object
     */
    public static function get_db_instance($db_name)
    {
        if (isset(self::$_db_map[$db_name]) && self::$_db_map[$db_name]) {
            return self::$_db_map[$db_name];
        }
        $CI = &get_instance();
        $db_handler = $CI->load->database($db_name, true);
        self::$_db_map[$db_name] = $db_handler;
        return $db_handler;
    }


    /**
     * @param $params
     *
     * @return mixed
     */
    private function __find($params)
    {
        if (isset($params['fields'])) {
            $this->db->select($params['fields']);
        }
        if (isset($params['where'])) {
            $this->db->where($params['where']);
        }
        if (isset($params['whereString'])) {
            $this->db->where($params['whereString']);
        }
        if (isset($params['or_where'])) {
            $this->db->or_where($params['or_where']);
        }
        if (isset($params['where_in'])) {
            foreach ($params['where_in'] as $k => $v) {
                $this->db->where_in($k, $v);
            }
        }
        if (isset($params['order_by'])) {
            foreach ($params['order_by'] as $k => $v) {
                $this->db->order_by($k, $v);
            }
        }
        if (isset($params['limit']) && isset($params['offset'])) {
            $this->db->limit($params['limit'], $params['offset']);
        }
        if (isset($params['limit']) && !isset($params['offset'])) {
            $this->db->limit($params['limit']);
        }
        if (isset($params['like']))
        {
            $this->db->like($params['like']);
        }
        $stmt = $this->db->get($this->tbName);
        if ($stmt === false) {
            log_message('error', sprintf('code:[%s] msg:[%s] sql:[%s]',
                $this->db->error()['code'],
                $this->db->error()['message'],
                $this->db->last_query()));
            return false;
        }
        return $stmt;
    }

    public function getUuid()
    {
        $sql = 'select uuid() as id';
        $row = $this->db->query($sql)->row_array();
        $uuid = strtoupper($row['id']);
        return $uuid;
    }
}
